package cjlu.ea.h.controller;


import cjlu.ea.h.common.AuthUtil;
import cjlu.ea.h.common.R;
import cjlu.ea.h.entity.Bjxx;
import cjlu.ea.h.entity.SUser;
import cjlu.ea.h.service.F201BjxxService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-09-18
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/h/bjxx")
public class F201BjxxController {
    @Autowired
    F201BjxxService bjxxService;
    @Autowired
    RedisTemplate redisTemplate;


    //获取班级数据
    @GetMapping("/getbjxx")
    public R getbjxx(){
        List<Bjxx> list = bjxxService.list(null);
        System.out.println(list);
        return R.ok().data("list",list);
    }

    @GetMapping("/getAllPagebjxx/{current}/{limit}")
    public R getAllPagebjxx(@PathVariable Long current, @PathVariable Long limit){

        //分页显示
        Page<Bjxx> page = new Page<>(current,limit);
        bjxxService.page(page,null);

        //数据查询获取
        List<Bjxx> list = page.getRecords();
        Long total = page.getTotal();

        return R.ok().data("list",list).data("total",total);
    }

    //分页条件查询
    @PostMapping("/getPagebjxxBytj/{current}/{limit}")
    public R getPagebjxxBytj(@PathVariable Long current,@PathVariable Long limit,
                             @RequestBody(required = false) Bjxx bjxx){

        List<Object> res = bjxxService.getbjxx(bjxx,current,limit);


        return R.ok().data("list",(List<Bjxx>)res.get(0)).data("total",(Long)res.get(1));
    }

    @GetMapping("/getAllPagebjxxBytj/{current}/{limit}")
    public R getAllPagebjxxBytj(@PathVariable Long current,@PathVariable Long limit){

        //分页显示
        Page<Bjxx> page = new Page<>(current,limit);
        bjxxService.page(page,null);

        //数据查询获取
        List<Bjxx> list = page.getRecords();
        Long total = page.getTotal();

        return R.ok().data("list",list).data("total",total);
    }

    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id, HttpServletRequest request){
        AuthUtil authUtil = new AuthUtil();
        authUtil.setRedisTemplate(redisTemplate);
        String role = authUtil.getAuthentications(request);
        if("admin".equals(role)){
            log.info("admin 可操作...");
            boolean result = bjxxService.removeById(id);
            if (result){
                return R.ok().message("删除成功");
            }else{
                return R.error().message("删除失败");
            }
        }else{
            log.error("普通用户无权限操作 学院班级表....");
            return R.error().message("无权限操作");
        }



    }

    @GetMapping("getbjxxById/{id}")
    public R getbjxxById(@PathVariable Integer id){
        return R.ok().data("bj",bjxxService.getById(id));
    }

    @PostMapping("update")
    public R updatebjxxById(@RequestBody Bjxx bjxx){
        Bjxx newbjxx = new Bjxx();
        BeanUtils.copyProperties(bjxx,newbjxx);
        bjxxService.updateById(newbjxx);

        return R.ok();
    }

    //查找所有学院
    @GetMapping("/getAllDepartment")
    public R getAllDepartment(){
        List<String> list = bjxxService.getXyList();
        return R.ok().data("xylist",list);
    }


    //按照院查询
    @GetMapping("/getByDepartment/{department}")
    public R getByDepartment(@PathVariable String department){
        QueryWrapper<Bjxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("XYMC",department);
        List<Bjxx> list = bjxxService.list(queryWrapper);
        List<String> zylist = bjxxService.getZyList(list);
        List<String> bjlist = bjxxService.getbjList(list);

        return R.ok().data("zyList",zylist).data("bjList",bjlist);
    }
    //新增按钮
    @PostMapping("saveOrUpdateOne")
    public R saveOrUpdateBysById(@RequestBody Bjxx bjxx){
        Bjxx newbys = new Bjxx();
        BeanUtils.copyProperties(bjxx,newbys);
        List<Bjxx> list = new ArrayList<>();
        list.add(newbys);
        bjxxService.saveOrUpdateBatch(list);
        return R.ok();
    }

}

